package com.kh.pajx.sop.view.action;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.SysUserGroup;
import com.kh.pajx.sop.util.common.DwzAjaxResult;
import com.kh.pajx.sop.util.common.ValidatorUtils;
import com.kh.pajx.sop.util.page.QueryHelper;
import com.opensymphony.xwork2.ActionContext;

/**
 * 用户组信息Struts2逻辑控制器
 * @author:	taller & azzcsimp
 * @Createdate:	2014年7月17日 下午8:28:49
 */
@Controller
@Scope("prototype")
public class SysUserGroupAction extends ModelDrivenBaseAction<SysUserGroup> {

	private static final long serialVersionUID = -1460467030742447520L;
	
	/** 列表 */
	public String list(){
		new QueryHelper(SysUserGroup.class,"usg")
			.addOrderProperty("usg.usgCode", true)
			.preparePageBean(sysUserGroupService, pageNum, numPerPage);
		return "list";
	}
	
	/** 添加页面 */
	public String addUI(){
		return "saveUI";
	}
	
	/** 添加 */
	public void add(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			validatePropertitysByAdd(model);//安全验证
			sysUserGroupService.save(model);
			dwz.setMessage("保存成功！");
			dwz.setNavTabId("userGroup_list");
			dwz.setStatusCode("200");
			dwz.setCloseDialog("userGroup_addUI");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("保存失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 修改 */
	public String editUI(){
		SysUserGroup userGroup = sysUserGroupService.getById(model.getUsgId());
		ActionContext.getContext().getValueStack().push(userGroup);
		return "saveUI";
	}
	
	/** 修改 */
	public void edit(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			validatePropertitysByUpdate(model);//安全验证
			SysUserGroup userGroup = sysUserGroupService.getById(model.getUsgId());
			userGroup.setUsgName(model.getUsgName());
			sysUserGroupService.update(userGroup);
			dwz.setMessage("修改成功!");
			dwz.setNavTabId("userGroup_list");
			dwz.setStatusCode("200");
			dwz.setCloseDialog("userGroup_editUI");
			DwzAjaxResult.josnMessage(dwz);
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("修改失败！<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	/** 删除 */
	public void delete(){
		DwzAjaxResult dwz = new DwzAjaxResult();
		try {
			if(!ValidatorUtils.validateUUID(model.getUsgId(),false)){
	    		throw new Exception("属性[用户组编号]输入错误，非法提交！");
	    	}
			SysUserGroup userGroup = sysUserGroupService.getById(model.getUsgId());
			long count = sysUserRoleService.countUserRoleByGroupCode(userGroup.getUsgCode());
			if (count > 0){	
				dwz.setMessage("删除失败!<br>错误信息：当前用户组下包含<font color='red'>"+count+"</font>条用户角色信息数据！");
				dwz.setStatusCode("300");
				DwzAjaxResult.josnMessage(dwz);
			}else{
				sysUserGroupService.delete(model.getUsgId());
				dwz.setMessage("删除成功!");
				dwz.setNavTabId("userGroup_list");
				dwz.setStatusCode("200");
				DwzAjaxResult.josnMessage(dwz);
			}
		} catch (Exception e) {
			e.printStackTrace();
			dwz.setMessage("删除失败!<br>错误信息："+e.getMessage());
			dwz.setStatusCode("300");
			DwzAjaxResult.josnMessage(dwz);
		}
	}
	
	
	
	/** 查看 */
	public String show(){
		return "";
	}
	
	
	/**安全验证*/
	private void validatePropertitysByAdd(SysUserGroup demo)throws Exception{
    	if(!ValidatorUtils.validateSafeString(demo.getUsgName(), 20, false)){
    		throw new Exception("属性[用户组名称]输入错误，非法提交！");
    	}
    }
	private void validatePropertitysByUpdate(SysUserGroup demo)throws Exception{
		if(!ValidatorUtils.validateUUID(demo.getUsgId(),false)){
    		throw new Exception("属性[用户组编号]输入错误，非法提交！");
    	}
		if(!ValidatorUtils.validateSafeString(demo.getUsgName(), 20, false)){
    		throw new Exception("属性[用户组名称]输入错误，非法提交！");
    	}
    }

}
